<template>
    <view class="goods-list">
        <view v-for="(goods,i) in goodsList" :key="i" @click="gotoDetail(goods)">
            <my-goods :goods="goods"></my-goods>
        </view>
    </view>
</template>

<script>
    import myGoods from '@/components/my-goods/my-goods.vue'
    export default {
        components: {
            myGoods
        },
        data() {
            return {
                queryobj: {
                    query: '',
                    cid: '',
                    pagenum: 1,
                    pagesize: 10
                },
                goodsList: [],
                goodsTotal: 0,
                defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png',
                // 节流阀
                isloading: false
            };
        },
        onLoad(option) {
            this.queryobj.query = option.query || ''
            this.queryobj.cid = option.cid || ''
            
            console.log('处理后的查询参数:', this.queryobj)

            // 确保只调用一次getGoodsList
            this.getGoodsList()
        },
        methods: {
            async getGoodsList(cb) {
                // 打开节流阀
                this.isloading = true

                // 不使用解构，直接获取响应对象
                const res = await uni.$http.get('/api/public/v1/goods/search', this.queryobj)

                // 正确检查响应状态
                if (res.statusCode !== 200) {
                    this.isloading = false
                    uni.$showMsg()
                    cb && cb()
                    return
                }

                // 获取新数据并确保是数组
                const newGoods = Array.isArray(res.data?.message?.goods) ? res.data.message.goods : [];

                // 保存当前是否是分页加载
                const isPaginationLoad = this.queryobj.pagenum > 1;

                // 根据当前页码判断是替换还是合并数据
                if (isPaginationLoad) {
                    // 分页加载时合并数据
                    this.goodsList = [...this.goodsList, ...newGoods];

                    // 记录新数据加载前的总数量
                    const previousLength = this.goodsList.length - newGoods.length;

                    // 滚动到新加载内容的顶部
                    setTimeout(() => {
                        const itemHeight = 120; // 商品项高度
                        const scrollTop = previousLength * itemHeight;
                        uni.pageScrollTo({
                            scrollTop,
                            duration: 300
                        });
                    }, 100);
                } else {
                    // 首次加载时替换数据
                    this.goodsList = newGoods;
                }

                // 设置总数
                this.goodsTotal = res.data?.message?.total || 0;

                // 关闭节流阀
                this.isloading = false
                cb && cb()
            },
            gotoDetail(goods) {
                uni.navigateTo({
                    url: '/subpkg/goods_detail/goods_detail?goods_id=' + goods.goods_id
                })
            }
        },
        // 用户下拉触底
        onReachBottom() {
            // 首先检查是否正在加载
            if (this.isloading) {
                return;
            }
            // 使用实际已加载的商品数量与总数比较，同时确保total有有效值
            if (this.total > 0 && this.goodsList.length >= this.total) {
                uni.$showMsg('数据加载完毕');
                return;
            }

            // 让页码值自增加一
            this.queryobj.pagenum++
            this.getGoodsList()
        },
        // 上拉刷新
        onPullDownRefresh() {
            this.queryobj.pagenum = 1
            this.total = 0
            this.goodsList = []
            this.isloading = false

            // 重新发起数据请求
            this.getGoodsList(() => uni.stopPullDownRefresh())
        }
    }
</script>

<style lang="scss">

</style>